package com.xl6667.handler.exception;

import com.xl6667.domain.ResponseResult;
import com.xl6667.enums.AppHttpCodeEnum;
import com.xl6667.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobExceptionHandler  {
    @ExceptionHandler(SystemException.class)
    public ResponseResult systemException(SystemException e){
        log.error("出现了异常! " + e);
      return ResponseResult.errorResult(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(AuthenticationException.class)
    public ResponseResult authenticationException(AuthenticationException e){
        log.error("出现了异常！ " + e);
        if(e instanceof BadCredentialsException ){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR.getCode(),AppHttpCodeEnum.LOGIN_ERROR.getMsg());
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR,e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception e){
        log.error("出现了异常！ " + e);
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }



}
